# D:\dazuoye\app\__init__.py

from flask import Flask
from flask_cors import CORS
from .data_loader import prepare_all_data


def create_app():
    app = Flask(__name__)
    CORS(app)

    with app.app_context():
        print("🚀 正在准备应用数据...")
        if not prepare_all_data():
            print("❌ 数据加载失败，服务可能无法正常工作。")
        else:
            print("🎉 所有数据准备就绪！")

    # 从views文件夹导入所有蓝图
    from .views.task1_trends import task1_bp
    from .views.task2_campaigns import task2_bp
    from .views.task3_social import task3_bp
    from .views.task4_predict import task4_bp
    from .views.sales_channel import sales_bp
    from .views.sales_regional import regional_bp
    from .views.sales_customer import customer_bp
    from .views.sales_churn import churn_bp

    # 注册蓝图
    app.register_blueprint(task1_bp)
    app.register_blueprint(task2_bp)
    app.register_blueprint(task3_bp)
    app.register_blueprint(task4_bp)
    app.register_blueprint(sales_bp)
    app.register_blueprint(regional_bp)
    app.register_blueprint(customer_bp)
    app.register_blueprint(churn_bp)

    print("🚀 所有蓝图注册完毕，服务配置完成！")
    return app